﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dados.DSMPTUDOTableAdapters;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
namespace Dados
{
    public class AudioDAL
    {
        static T_AUDIOTableAdapter audioTA = new T_AUDIOTableAdapter();

        public static DSMPTUDO.T_AUDIODataTable ConsultaTodos()
        {
            try
            {
                return audioTA.GetData();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }
        public static DSMPTUDO.T_AUDIODataTable ConsultaPorUsuario(string usuario)
        {
            try
            {
                return audioTA.GetAudiosPorUsuario(usuario);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }
        public static DSMPTUDO.T_AUDIODataTable ConsultaPorCodigo(int codigo)
        {
            try
            {
                return audioTA.GetAudioPorCodigo(codigo);
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static bool Insere ( string nome, string localizacao, int genero, int autor, int album, int formato, int especificacao, int software, string usuario)
        {
            try
            {
                if (audioTA.Insert (nome, localizacao, genero, autor, album, formato, especificacao, software, usuario) ==1) 
                    return true;
                else return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static bool Atualiza(int codigo, string nome, string localizacao, int genero, int autor, int album, int formato, int especificacao, int software, string usuario) 
        {
            try
            {
                return (audioTA.Update(nome, localizacao, genero, autor, album, formato, especificacao, software, usuario, codigo) == 1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static bool Remove(int codigo)
        {
            try
            {
                return (audioTA.Delete(codigo) == 1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DSMPTUDO.T_AUDIODataTable ConsultaPorFiltro(String nome, int genero, int autor, int album, int formato)
        {
            if (nome == null) nome = "%";

            string consulta = ((String.Format("SELECT * FROM T_AUDIO WHERE tx_nome like '%{0}%'",
                    nome).ToString()) +
                    (autor != 0 ? " AND nr_autor = " + autor.ToString() : "") +
                    (album != 0 ? " AND nr_album = " + album.ToString() : "") +
                    (genero != 0 ? " AND nr_genero = " + genero.ToString() : "") +
                    (formato != 0 ? " AND nr_formato = " + formato.ToString() : ""));

            SqlConnection connection = new SqlConnection(audioTA.Connection.ConnectionString);
            SqlCommand cmd = new SqlCommand(consulta, connection);
            cmd.CommandType = System.Data.CommandType.Text;

            audioTA.Adapter.SelectCommand = cmd;
            DSMPTUDO.T_AUDIODataTable audioTable = new DSMPTUDO.T_AUDIODataTable();
            audioTA.Adapter.Fill(audioTable);
            return audioTable;
        }
    
         
         }
}